package com.komodo.island.gdx.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.actors.Image;
import com.komodo.island.gdx.Art;
import com.komodo.island.gdx.KomodoIsland;
import com.komodo.island.gdx.Persistence;
import com.komodo.island.gdx.dialog.Dialog;

public class FactsScreen extends StoryScreen {

	public int factNumber;

	public FactsScreen(TextureRegion[] scene, int sceneNumber, double interval,
			Screen nextScreen) {
		super(scene, sceneNumber, interval, nextScreen);
		// TODO Auto-generated constructor stub
		// this.factNumber = factNumber;
	}

	Dialog facts;

	@Override
	public void init(KomodoIsland komodoIsland, Texture texture) {
		// TODO Auto-generated method stub
		super.init(komodoIsland, texture);
		// this.previousScreen = komodoIsland.mainMenuScreen;
		if (scene == null) {
			scene = new TextureRegion[7];
			// if (factNumbeR==0){
			scene[0] = new TextureRegion(texture, 0, 0, 320, 240);
			// }else if (factNumber==1){
			scene[1] = new TextureRegion(texture, 320, 0, 320, 240);
			// }else if (factNumber==2){
			scene[2] = new TextureRegion(texture, 320 * 2, 0, 320, 240);
			// }else if (factNumber==3){
			scene[3] = new TextureRegion(texture, 0, 240, 320, 240);
			// }else if (factNumber==4){
			scene[4] = new TextureRegion(texture, 320, 240, 320, 240);
			// }else if (factNumber==5){
			scene[5] = new TextureRegion(texture, 320 * 2, 240, 320, 240);
			// }else if (factNumber==6){
			scene[6] = new TextureRegion(texture, 0, 240 * 2, 320, 240);
			// }
		}

		if (facts == null)
			facts = new Dialog(480, 320, true, this, komodoIsland.factTexture,komodoIsland) {
				Image bg;

				// Image desc;

				@Override
				public void setThePosition() {
					// TODO Auto-generated method stub
					addActor(bg);
					// addActor(desc);
					// desc.x = 143;
					// desc.y = 70;

				}

				@Override
				public void setTheListener() {
					// TODO Auto-generated method stub

				}

				@Override
				public void init() {
					// TODO Auto-generated method stub
					bg = new Image("bg", new TextureRegion(texture, 960, 480,
							480, 320));
					// desc = new Image("desc", scene[factNumber]);
				}

				@Override
				public void addElements() {
					// TODO Auto-generated method stub

				}
			};

		// batch = facts.getSpriteBatch();

	}

	@Override
	public void onScreenRun() {
		// TODO Auto-generated method stub

		if (currScene < sceneNumber) {
			currTime = System.currentTimeMillis();
			if (currTime - startTime <= interval) {
				if (scene != null) {
					facts.draw();
					facts.getSpriteBatch().begin();
					facts.getSpriteBatch().draw(scene[factNumber], 143, 70);

					facts.getSpriteBatch().end();

				}
				// Art.drawString(currScene + "", 0, 0);

				if (Gdx.input.justTouched()) {
					if (Persistence.sound)
						komodoIsland.dialogSound.play(1);
					startTime = currTime;
					currScene++;
				}

			} else {
				startTime = currTime;
				currScene++;
			}
		} else {
			try {
				komodoIsland.goToScreen(nextScreen);
			} catch (Exception e) {
				komodoIsland.goToScreen(komodoIsland.mainMenuScreen);
			}
		}
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();

	}

	@Override
	public void cleanResScreen() {
		// TODO Auto-generated method stub
		super.cleanResScreen();
		try {
			facts.dispose();
		} catch (Exception e) {

		}
	}

	public void setFact(int number) {
		factNumber = number;
		restartStory();
	}

}
